/**
 * 做法
 * 2019-12-14
 */
;(function ($) {
	//入口
	$.fn.service_way=function(options){
    	//创建对象
		var $ele = new way(this, options);
		//入口
		$ele.html();
	}
	
	//构造函数
    var way = function(ele, opt) {    	
    	//选择器对象    	
        this.$element=ele;   
        //默认参数
        this.defaults = {
    		dataBack:function(){},
    		returnBack:function(){},
        },        
        //初始化参数
        this.options = $.extend({}, this.defaults, opt);
    }
    
    //属性方法
    way.prototype = {
    	//创建模态
    	html:function(){
    		var $t=this;
    		var $opt=$t.options;
    		var el=this.$element;
    		
    		el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="modal-dialog" style="width:calc( 80% - 130px)">'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<span id="modal-h4" style="font-size:20px;font-weight:bold;">'+$.i18n.propJsp("做法")+'</span>'+ 
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="max-height:420px;">'+ 
									'<div class="typeList" style="max-height:500px;overflow-y:auto;overflow-x:hidden;padding:5px 0;"></div>'+
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="clearifx">'+
										'<div class="pull-right">'+
											'<div class="btn-group">'+
												'<button id="modalBtnClose" class="btn btn-default">'+
													'<i class="ace-icon fa fa-times"></i>'+
													$.i18n.propJsp("关闭")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnSure" class="btn btn-info">'+  
													'<i class="ace-icon fa fa-check"></i>'+ 
													$.i18n.propJsp("确定")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
			
			//获取默认数据
			$t.getDefaultValue(); 
    	},
    	//默认数据
    	getDefaultValue:function(){
    		var $t=this;
    		var $opt=$t.options;
    	    var el=$t.$element;
    		
    		var param={"arg":{"arg":"作法"}};
 			jQuery.ajax({
 				type : 'POST',
 				url : $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
 				data: $.HSAPI_ASKJSONINFO(JSON.stringify(param),'POSMenuMessage','{}','list'),     
 				dataType : 'json',  
 				success : function(data){
// 					alert(console.log(JSON.stringify(data)))  
 					$(data).each(function(k,v){
 						var w = el.find(".modal-header").width();
 						var width = w-30;
 						var height = (width/6-4)*2/5*2;
 						html=$t.makeDetails(v,width,height,6,k);
 						$(html).appendTo(el.find(".typeList"));
 					});
 					$t.bindEvent();
 				},
 				error : function(data){  
 					systoast('POSMenuMessage:','系统后台提交错!' + JSON.stringify(data),'error')
 				}  
 			});
    	},
    	//构造色块
    	makeDetails:function(data,width,height,twidthNum,index){
    		var $t=this;
    		var $opt=$t.options;
    	    var el=$t.$element;
    	    
    	    var $type;
    		var thisclass="btn-light"//默认样式 
    		var fontsize=18//默认字体大小
    		var width=width/twidthNum-6//菜别类型宽度
    		var eachlength=data.mc.length//名称长度
    		var maxinclude=0//最大容量
    		var ifsmaller=""
    		maxinclude=width/fontsize
    		if($(window).width()>1024){
    			fontsize=18
    		}else{
    			fontsize=15
    		}
    		if(eachlength>=maxinclude){//超出容量
    			fontsize=width/eachlength
    			ifsmaller=" besmall"
    		}
    		$type=$('<div><button type="button" class="btn btn-app '+thisclass+' dishes_type_each" data-mc="'+ data.mc +'" data-index="'+index+'">'+
    						'<div class="type_content'+ifsmaller+'">'+ data.mc +'</div>'+
    					'</button></div>')
    		$type.find(".dishes_type_each").css("font-size",fontsize+"px")//设置字体大小
    		$type.find(".dishes_type_each").css("width",width).css("min-width",width)//设置宽度 
    		var hhhh = (height/2-6)>50 ? 50 : ((height/2-6)<30 ? 30 : (height/2-6))
    		$type.find(".dishes_type_each").css("height",hhhh)
    		$type.find(".dishes_type_each").css("padding",0)
    		$type.find(".dishes_type_each").css("text-align","center")
    		$type.find(".type_content").css("line-height",hhhh+"px")
    		$type.find(".type_content").css("width","100%").css("height","100%")
    		return $type.html()
    	},
    	//事件绑定
    	bindEvent:function(){
    		var $t=this;
    		var $opt=$t.options;
    	    var el=$t.$element;
    	    
    	    //确定
    	    el.find("#modalBtnSure").bind("click",function(){
    	    	var zf=""
    	    	el.find(".dishes_type_each").each(function(k,v){  
    	    		if($(this).hasClass("btn-primary"))
    	    			zf+=(zf=="")?$(v).attr("data-mc"):","+$(v).attr("data-mc");
    	    	}); 
    	    	$opt.dataBack(zf);
    	    });
    	    
    	    //关闭
    	    el.find("#modalBtnClose").bind("click",function(){
    	    	$opt.closeBack(); 
    	    });
    	    
    	    //色块点击
    	    el.find(".dishes_type_each").bind("click",function(){
    	    	if($(this).hasClass("btn-light")){
    	    		$(this).removeClass("btn-light");
    	    		$(this).addClass("btn-primary");
    	    		return;
    	    	} 
    	    	if($(this).hasClass("btn-primary")){
    	    		$(this).removeClass("btn-primary");
    	    		$(this).addClass("btn-light");
    	    	}	 
    	    });
    	},
		//返回筛选器对象,用于链式操作
    	hsOle: function() {
            return this.$element;      
        },
        //唯一标识
        uuid:function(){
        	var oDate = new Date(); 
			var year=oDate.getFullYear();   
			var month=oDate.getMonth()+1;   
			var day=oDate.getDate(); 
			var hour=oDate.getHours(); 
			var min=oDate.getMinutes(); 
			var sen=oDate.getSeconds();
			var x = 32767;     
		    var y = 0;     
		    var rand = parseInt(Math.random() * (x - y + 1) + y);
		    var newId=year+""+month+""+day+""+hour+""+min+""+sen+""+rand;
		    return newId;
        }
    }
})(jQuery);